import Vue from 'vue'
import Router from 'vue-router'
import Layout from '_layout' // 整体架构
Vue.use(Router)

// 基本路由
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/Dashboard/index'),
      meta: { title: 'Dashboard', icon: 'el-icon-s-unfold' }
    }]
  },
  {
    path: '/itemLists',
    redirect: '/itemLists/index',
    component: Layout,
    meta: {
      icon: 'el-icon-s-goods',
      title: '商品管理'
    },
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/ItemLists/index'),
      meta: {
        icon: 'el-icon-s-goods'
      },
      children: [{
        path: 'itemAdd',
        name: 'itemAdd',
        hidden: true,
        component: () => import('@/views/ItemAdd/index'),
        meta: {
          title: '增加商品'
        }
      },
      {
        path: 'itemUpdate/:id',
        name: 'itemUpdate',
        hidden: true,
        component: () => import('@/views/ItemUpdate/index'),
        meta: {
          title: '修改商品'
        }
      }]
    }]
  },
  {
    path: '/message',
    component: Layout,
    children: [{
      path: 'index',
      name: 'message',
      component: () => import('@/views/MsgList/index'),
      meta: {
        icon: 'el-icon-message-solid',
        title: '消息中心'
      }
    }]
  },
  {
    path: '/setting',
    component: Layout,
    children: [{
      path: 'index',
      name: 'setting',
      component: () => import('@/views/Setting/index'),
      meta: {
        icon: 'el-icon-s-tools',
        title: '设置'
      }
    }]
  },
  {
    path: '/personal',
    component: Layout,
    hidden: true,
    children: [{
      path: 'index',
      name: 'personal',
      component: () => import('@/views/Personal/index'),
      meta: {
        icon: 'el-icon-s-tools',
        title: '个人中心'
      }
    }]
  }
]
// 基于用户权限 动态加载的路由
export const asycnRoute = [
  {
    path: '/permission',
    component: Layout,
    alwaysShow: true,
    redirect: '/permission/page',
    name: 'Permission',
    meta: {
      title: '权限',
      icon: 'el-icon-s-cooperation',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/Permission/page'),
        name: 'PagePermission',
        meta: {
          title: '页面权限',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/Permission/directive'),
        name: '指令权限',
        meta: {
          title: '指令权限'
          //
        }
      },
      {
        path: 'role',
        component: () => import('@/views/Permission/role'),
        name: '角色权限',
        meta: {
          title: '角色权限',
          roles: ['admin']
        }
      },
      {
        path: '*',
        name: '404',
        component: () => import('@/views/NotFound/index'),
        meta: {
          icon: 'shop',
          title: '404'
        },
        hidden: true
      }
    ]
  }
]
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
